package com.bdqn.service.platform;

import bean.PlatformMember;

import com.alibaba.fastjson.JSONObject;
import com.bdqn.common.utils.ResponseUitl;
import com.bdqn.common.utils.VerifyCodeUtil;

import com.bdqn.dao.entity.Platform;
import com.bdqn.service.platform.PlatformParent.platformparent;
import com.bdqn.service.platformlist.push.JiGuangPushUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class Shuangzuan extends platformparent {
    /**
     * 缓存接封装口
     */
//    @Autowired
//    CacheService cacheService;

    /**
     *任务订单封装接口
     */
//    @Autowired
//    OrderService orderService;

    //极光推送实现类
    JiGuangPushUtil uni_push=new JiGuangPushUtil();

    /**
     * 登录地址
     */
    String loginApi = "/mobile/login.aspx";
    /**
     * 获取买号
     */
    String listPlatformAccountApi = "/mobile/BindTb.aspx";
    /**
     * 接单
     */
    String acceptOrderApi = "/Mobile/API/V1/AutoTask/";
    /**
     * 首页
     */
    String indexApi="/mobile/index.aspx";
    /**
     * 接单信息
     */
    String orderInfoApi="/mobile/Mytasklist.aspx";

    String accessToken = "";
    //登录用户的ID
    String loginUserId="";

    Map<String,List<Object>> result_map;
    //买号信息
    List<Object> result_list;

    /**
     * 添加任务平台获取买号
     * @param platformMember
     * @return
     */

    public Map<String,List<Object>> process(PlatformMember platformMember, Object... objects) {
        //载入缓存
//        cacheService= (CacheService) objects[0];
        //实现登录
        int status=login(platformMember);
        if(status == VerifyCodeUtil.LOGIN_ERROR){
            return new HashMap<String,List<Object>>(){
                {
                    put("登录 "+platformMember.getPlatformName()+" 失败/账号密码错误", Collections.emptyList());
                }
            };
        }
        //调用自动获取买号
        status=getPlatformAccount(platformMember);
        if(status == VerifyCodeUtil.BUYER_ERROR){
            return new HashMap<String,List<Object>>(){
                {
                    put("获取 "+platformMember.getPlatformName()+"平台 获取买号失败/无审核的买号!",Collections.emptyList());
                }
            };
        }

        if(platformMember.getClientId()!=null && !"".equals(platformMember.getClientId())) {
            //调用获取用户ID
//            status = getUserId(platformMember);
        }
        return result_map;
    }


    /**
     * 登录
     * @param platformMember
     */
    public int  login(PlatformMember platformMember){
       // String loginApi = "/mobile/login.aspx";
     //   platformMember.setUrl(url.substring(0,platformMember.getUrl().length()-18));
        Map<String,String> map =new HashMap<>();
        map.put("mobile",platformMember.getAccount());
        map.put("password",platformMember.getPassword());
//        HashMap result =  ResponseUitl.getByAuthorization(platformMember.getUrl()+loginApi,null,map,"utf-8");
        List<String> cookieStr = ResponseUitl.getCookieByDoPost(platformMember.getUrl()+loginApi,null,map,"utf-8");
        System.out.println(cookieStr);
        accessToken=cookieStr.get(1);
        if("false".equals(ResponseUitl.pullMessage(cookieStr.get(0),"IsSuccess"))){
//            使用推送将消息推送出去，执行失败
            uni_push.transmit(platformMember.getClientId(),
                    uni_push.productionMessage(platformMember.getNamePinyin(),
                            "登录"+platformMember.getPlatformName()+"平台失败/账号密码错误",VerifyCodeUtil.LOGIN_ERROR));
            return -1;
        }
            uni_push.transmit(platformMember.getClientId(),
                    uni_push.productionMessage(platformMember.getNamePinyin(),
                            "成功登录"+platformMember.getPlatformName()+"平台",VerifyCodeUtil.LOGIN_SUCCESS));
            return 1;
    }


    /**
     * 自动获取买号
     * @return
     */
    public int getPlatformAccount(PlatformMember platformMember){
        Map<String,String> paramsMap =new HashMap<>();
        paramsMap.put("Cookie",accessToken);
        List<String> list= ResponseUitl.getCookieByDoPost("https://m.wakavip.com/Mobile/AutoTask.aspx",paramsMap,null,"utf-8");

        result_list=new ArrayList<>();
        String result=list.get(0);//获取cookie内的第一个值
        if(null == result || "".equals(result)){//判断是否获取成功
            return -2;
        }

        //开始获取买号
        Map<String,Object> value_map=new HashMap<>();
        if(result!=null && !"".equals(result)){
            //定义实体类
            Platform platform=null;
            Document document = Jsoup.parse(result);
            //获取买号，第一次截取
            Element postList = document.getElementById("ddlAccount");
            System.out.println(postList);
            //获取买号，第二次截取
            Elements postItems = postList.select("option[id]");
            for (Element postItem : postItems) {
                String text = postItem.text();
                platform=new Platform();
                platform.setName(text);
                result_list.add(platform);

            }
        }
        if(platformMember.getClientId()!=null && !"".equals(platformMember.getClientId())){
            JSONObject jsonObject = (JSONObject)JSONObject.toJSON(platformMember);
//            //缓存 买号信息
//            cacheService.hset(platformMember.getMemberId()+"_"+platformMember.getNamePinyin(),"buyerInfo", JSONObject.toJSONString(value_map));
//            //缓存 登录状态
//            cacheService.hset(platformMember.getMemberId()+"_"+platformMember.getNamePinyin(),"token", accessToken);
//            //缓存 接单次数  最大 20 次 逐次递减
//            cacheService.hset(platformMember.getMemberId()+"_"+platformMember.getNamePinyin(),"orderCount", "10");
        }
        uni_push.transmit(platformMember.getClientId(),uni_push.productionMessage(platformMember.getNamePinyin(),"获取到"+result_list.size()+"个买号",VerifyCodeUtil.BUYER_SUCCESS));

        result_map = new HashMap<>();
        result_map.put("获取 "+platformMember.getPlatformName()+" 平台买号成功!",result_list);
        return 1;
    }
}
